import Vue from 'vue'
import Router from 'vue-router'
import list from '@/view/list'
import cart from '@/view/cart'
import product from '@/view/product'

Vue.use(Router)

const routers = new Router({
  // 使用 HTML5 的 History 路由模式
  mode: 'history',
  routes: [
    {
      path: '/list',
      name: 'list',
      meta:{
        title:"列表"
      },
      component: list
    },
    {
      path: '/cart',
      name: 'cart',
      meta:{
        title:"购物车"
      },
      component: cart
    },
    {
      path: '/product/:id',
      name: 'product',
      meta:{
        title:"详情页"
      },
      component: product
    },
    {
      path:"*",
      redirect:"/list"
    }
  ]
})

routers.beforeEach((to, from, next) => {
    window.document.title = to.meta.title;
    next();
});
routers.afterEach((to, from, next) => {
    window.scrollTo(0, 0);
});

export default routers;
